package com.followme.md5.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.followme.md5.bean.Person;
import com.followme.md5.dao.IPersonDAO;
import com.followme.md5.impl.PersonDAOImpl;

public class RegisterServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");

		String pid = request.getParameter("pid");
		String pwd = request.getParameter("pwd");

		System.out.println("pid:" + pid + "pwd:" + pwd);

		Person person = new Person();
		person.setPid(pid);
		person.setPwd(pwd);

		System.out.println(person);

		try {
			IPersonDAO personDao = new PersonDAOImpl();
			boolean flag = personDao.addPerson(person);
			if (flag) {
				request.getRequestDispatcher("login.jsp").forward(request,
						response);
			} else {
				request.getRequestDispatcher("error.jsp").forward(request,
						response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		doGet(req, resp);
	}

	@Override
	public void destroy() {

		super.destroy();
	}
}
